/**
 * Created by 陈熠 on 2017/6/21
 * email   :  228112142@qq.com
 */
(function ($) {

    /*默认配置*/
    var defaultProps = {
        url: "",     //数据源 url
        codeName: "",//数据源 codeName
        enumName: "",//数据源 enumName
        name: "",    //表单name
        value: "",   //默认值
        required: true,//是否必选
        filter: "",  //监听标识
        disabled: false,//1.true禁用组件2.字符串值禁用某个值逗号隔开 如"1,2,3"
        tips: '请选择'  //提示
    };

    /* 入口函数 */
    $.fn.renderSelect = function (props) {
        //当前对象
        var $this = this;
        //当前对象上的参数,自动渲染时使用该 参数
        var cyProps = $this.attr("cyProps") || "";
        //如果是方法渲染，优先使用方法中的参数
        props = props ? props : eval("({" + cyProps + "})") || {};
        props = $.extend({}, defaultProps, props);
        var url = props.url, //从后台获取数据
            codeName = props.codeName,  //从表码获取数据
            enumName = props.enumName;  //从枚举获取数据
        var R = url && $t.getDataByUrl(url) || codeName && $t.getDataByCode(codeName) || enumName && $t.getDataByEnum(enumName);
        if(R===""){return false}
        selectFuc.createDom(R, $this, props);
    };


    /*方法对象*/
    var selectFuc = {
        /**渲染下拉框数据 by chenyi 2017/6/21*/
        createDom: function (R, $this, props) {
            $this.html("");
            //获取下拉控件的name
            var _name = props.name;
            //获取下拉控件的默认值
            var _value = props.value;
            //是否必填
            var _required = props.required;
            //获取是否有提示
            var _tip = props.tips;

            //获取监控标识
            var _filter = props.filter;
            //disabled为true时禁用下拉框，否者禁用值
            var _disabled = props.disabled;

            var _select = '<select name="' + _name + '" lay-search ></select>';
            //是否必选
            if (_required === true) {
                _select = _select.replace("<select", "<select lay-verify='required'");
            }
            //添加监控标识
            if (_filter !== "") {
                _select = _select.replace("<select", "<select lay-filter=" + _filter);
            }
            //禁用组件
            if (_disabled === true) {
                _select = _select.replace("<select", "<select disabled");
            }
            $this.append(_select);
            //设置tips
            $this.find("select").append('<option value="">' + _tip + '</option>');

            var data = R.data;
            if (data !== undefined) {
                for (var i = 0; i < data.length; i++) {
                    var _option = '<option value="' + data[i].code + '">' + data[i].value + '</option>';
                    //设置默认值
                    if (_value === data[i].code) {
                        _option = _option.replace("<option", "<option selected ")
                    }
                    //设置禁用
                    if (typeof (_disabled) === "string") {
                        if (_disabled.split(",").indexOf(data[i].code) !== -1) {
                            _option = _option.replace("<option", "<option disabled ")
                        }
                    }

                    $this.find("select").append(_option);
                }
            }
            //渲染下拉框
            layui.use(['form'], function () {
                var form = layui.form;
                form.render('select');
            });

        }
    }

})(jQuery);

$(function () {
    $("[cyType='select']").each(function (index, item) {
        $(item).renderSelect();
    });
});